package com.budwk.app.sqfwV2.generator.dayingRoadPdf;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.AccommodatePeopleVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.SandwichingVo;
import com.budwk.app.sqfwV2.enums.dayingRoad.OtherPatrolContentsEnum;
import com.budwk.app.util.poiUtils.SitePictureOfficeUtils;
import com.deepoove.poi.XWPFTemplate;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图片附件
 */

@Slf4j
public class PictureGenerator extends BaseTemplatePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     */
    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return false;
    }

    @Override
    public String getTemplateName() {
        return null;
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
    }

    private void buildImage(String imageUrl, String text, List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList,
                       Sqfw_formSummary_new formSummary, FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        if (StringUtils.isEmpty(imageUrl)) {
            return;
        }
        if (imageUrl.startsWith("[") && imageUrl.endsWith("]")) {
            return;
        }
        List<String> pictureList = Lists.newArrayList(Lists.newArrayList(imageUrl.split(",")));
        String textStr = StrFormatter.format(text, pdfDTO.getPlaceName(), DateUtil.format(formSummary.getCreateDate(), "yyyy-MM-dd"));
        sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
    }

    private void buildSandwichingVoImage(SandwichingVo s, List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList,
            Sqfw_formSummary_new formSummary, FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        if (s == null) {
            return;
        }
        // 夹层楼板
        buildProblemImage(s.getMezzaninFfloorSlab(), "夹层楼板", sitePictureContentList, formSummary, pdfDTO);
        // 楼梯建筑材料
        buildProblemImage(s.getStaircaseBuildingMaterials(), "楼梯建筑材料", sitePictureContentList, formSummary, pdfDTO);
    }

    private void buildAccommodatePeopleImage(AccommodatePeopleVo s, List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList,
            Sqfw_formSummary_new formSummary, FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        if (s == null) {
            return;
        }
        buildProblemImage(s.getResidentialAreas(), "场所住人形式-经营与住区域是否有防火分隔", sitePictureContentList, formSummary, pdfDTO);
        // 违规住人照片
        String violationPhoto = s.getViolationPhoto();
        if (StringUtils.isNotEmpty(violationPhoto)) {
            buildImage(violationPhoto, "场所住人形式-违规住人照片附件：{} {} 隐患整改照片", sitePictureContentList, formSummary, pdfDTO);
        }
        // 整改后照片
        String rectificationAfterPhoto = s.getRectificationAfterPhoto();
        if (StringUtils.isNotEmpty(rectificationAfterPhoto)) {
            buildImage(rectificationAfterPhoto, "场所住人形式-整改后照片附件：{} {} 隐患整改照片", sitePictureContentList, formSummary, pdfDTO);
        }
    }

    private void buildProblemImage(SafeCheckTaskParamDataDTO p, String text, List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList,
                                   Sqfw_formSummary_new formSummary, FireSafetyOnDayingRoadPdfDTO pdfDTO) {
        if (p != null) {
            buildImage(p.getRiskImages(),  "附件：{} {} 隐患整改前照片【"+ text +"】", sitePictureContentList, formSummary, pdfDTO);
            buildImage(p.getRectificationBackPhoto(), "附件：{} {} 隐患整改后照片【"+ text +"】", sitePictureContentList, formSummary, pdfDTO);
        }
    }

    @Override
    public ByteArrayOutputStream generatePDFAfter(XWPFTemplate xwpfTemplate, Sqfw_formSummary_new formSummary) {
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList = Lists.newArrayList();
        FireSafetyOnDayingRoadPdfDTO pdfDTO = JSONUtil.toBean(formSummary.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
        String visitPictures = pdfDTO.getVisitPictures();
        if (StringUtils.isNotBlank(visitPictures)) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(visitPictures.split(",")));
            String textStr = "附件：{} {} 走访照片";
            textStr = StrFormatter.format(textStr, pdfDTO.getPlaceName(), DateUtil.format(formSummary.getCreateDate(), "yyyy-MM-dd"));
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }
        // 2 夹层搭建使用
        SandwichingVo s = pdfDTO.getSandwichConstructionUse();
        // 场所住人
        buildSandwichingVoImage(s, sitePictureContentList, formSummary, pdfDTO);
        buildAccommodatePeopleImage(pdfDTO.getAccommodatePeople(), sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getFireHouseAlarmSystem(), "火宅自动报警系统", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getAutomaticSprinklerSystem(), "自动喷水灭火系统", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getIndoorFireHydrant(), "室内消防栓", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getFireOtherIssues(), "消防设施的其他问题", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getElectricalCircuitSituation(), "电气线路情况", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getElectricBicyclesUse(), "电动自行车使用情况", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getFireHighUsageArea(), "明火、高温加热、取暖设备等的使用地点", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getIsCombustiblEexplosive(), "是否存放易燃易爆危险品", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getIsBlackAlcoholUsed(), "是否使用黑酒精", sitePictureContentList, formSummary, pdfDTO);
        List<SafeCheckTaskParamDataDTO> otherHiddenDangers = pdfDTO.getOtherHiddenDangers();
        if (otherHiddenDangers != null && otherHiddenDangers.size() > 0) {
            buildProblemImage(otherHiddenDangers.get(otherHiddenDangers.size() - 1), "其他隐患", sitePictureContentList, formSummary, pdfDTO);
        }
        buildProblemImage(pdfDTO.getGasAlarmsNumber(), "燃气报警器的数量", sitePictureContentList, formSummary, pdfDTO);
        buildProblemImage(pdfDTO.getIsAlarmNormal(), "燃气报警器是否正常", sitePictureContentList, formSummary, pdfDTO);
        // 其余巡查内容
        List<SafeCheckTaskParamDataDTO> others = pdfDTO.getOtherPatrolContents();
        for (SafeCheckTaskParamDataDTO other : others) {
            String desc = OtherPatrolContentsEnum.getDesc(other.getFields());
            String descStr = desc.substring(desc.indexOf(".")+1);
            buildProblemImage(other, descStr, sitePictureContentList, formSummary, pdfDTO);
        }
        log.info("image generatePDFAfter sitePictureContentList: {}", JSONObject.toJSONString(sitePictureContentList));
        ByteArrayOutputStream byteArrayOutputStream = null;

        if (sitePictureContentList.size() > 0) {
            byteArrayOutputStream = SitePictureOfficeUtils.createSitePicture(sitePictureContentList);
        }
        if (byteArrayOutputStream == null) return new ByteArrayOutputStream();
        return byteArrayOutputStream;
    }
}
